<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>SDL2_gfx: I:/Sources/sdl2gfx/SDL2_framerate.c Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">SDL2_gfx
   &#160;<span id="projectnumber">1.0.1</span>
   </div>
   <div id="projectbrief">GraphicsprimitivesandsurfacefunctionsforSDL2</div>
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.8.0 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">I:/Sources/sdl2gfx/SDL2_framerate.c</div>  </div>
</div><!--header-->
<div class="contents">
<a href="_s_d_l2__framerate_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"></span>
<a name="l00003"></a>00003 <span class="comment">SDL_framerate.c: framerate manager</span>
<a name="l00004"></a>00004 <span class="comment"></span>
<a name="l00005"></a>00005 <span class="comment">Copyright (C) 2001-2012  Andreas Schiffler</span>
<a name="l00006"></a>00006 <span class="comment"></span>
<a name="l00007"></a>00007 <span class="comment">This software is provided &#39;as-is&#39;, without any express or implied</span>
<a name="l00008"></a>00008 <span class="comment">warranty. In no event will the authors be held liable for any damages</span>
<a name="l00009"></a>00009 <span class="comment">arising from the use of this software.</span>
<a name="l00010"></a>00010 <span class="comment"></span>
<a name="l00011"></a>00011 <span class="comment">Permission is granted to anyone to use this software for any purpose,</span>
<a name="l00012"></a>00012 <span class="comment">including commercial applications, and to alter it and redistribute it</span>
<a name="l00013"></a>00013 <span class="comment">freely, subject to the following restrictions:</span>
<a name="l00014"></a>00014 <span class="comment"></span>
<a name="l00015"></a>00015 <span class="comment">1. The origin of this software must not be misrepresented; you must not</span>
<a name="l00016"></a>00016 <span class="comment">claim that you wrote the original software. If you use this software</span>
<a name="l00017"></a>00017 <span class="comment">in a product, an acknowledgment in the product documentation would be</span>
<a name="l00018"></a>00018 <span class="comment">appreciated but is not required.</span>
<a name="l00019"></a>00019 <span class="comment"></span>
<a name="l00020"></a>00020 <span class="comment">2. Altered source versions must be plainly marked as such, and must not be</span>
<a name="l00021"></a>00021 <span class="comment">misrepresented as being the original software.</span>
<a name="l00022"></a>00022 <span class="comment"></span>
<a name="l00023"></a>00023 <span class="comment">3. This notice may not be removed or altered from any source</span>
<a name="l00024"></a>00024 <span class="comment">distribution.</span>
<a name="l00025"></a>00025 <span class="comment"></span>
<a name="l00026"></a>00026 <span class="comment">Andreas Schiffler -- aschiffler at ferzkopp dot net</span>
<a name="l00027"></a>00027 <span class="comment"></span>
<a name="l00028"></a>00028 <span class="comment">*/</span>
<a name="l00029"></a>00029 
<a name="l00030"></a>00030 <span class="preprocessor">#include &quot;<a class="code" href="_s_d_l2__framerate_8h.html">SDL2_framerate.h</a>&quot;</span>
<a name="l00031"></a>00031 
<a name="l00037"></a><a class="code" href="_s_d_l2__framerate_8c.html#a3bed31ab61648f7d69c8f47c90161cfe">00037</a> Uint32 <a class="code" href="_s_d_l2__framerate_8c.html#a3bed31ab61648f7d69c8f47c90161cfe" title="Internal wrapper to SDL_GetTicks that ensures a non-zero return value.">_getTicks</a>()
<a name="l00038"></a>00038 {
<a name="l00039"></a>00039         Uint32 ticks = SDL_GetTicks();
<a name="l00040"></a>00040 
<a name="l00041"></a>00041         <span class="comment">/* </span>
<a name="l00042"></a>00042 <span class="comment">        * Since baseticks!=0 is used to track initialization</span>
<a name="l00043"></a>00043 <span class="comment">        * we need to ensure that the tick count is always &gt;0 </span>
<a name="l00044"></a>00044 <span class="comment">        * since SDL_GetTicks may not have incremented yet and</span>
<a name="l00045"></a>00045 <span class="comment">        * return 0 depending on the timing of the calls.</span>
<a name="l00046"></a>00046 <span class="comment">        */</span>
<a name="l00047"></a>00047         <span class="keywordflow">if</span> (ticks == 0) {
<a name="l00048"></a>00048                 <span class="keywordflow">return</span> 1;
<a name="l00049"></a>00049         } <span class="keywordflow">else</span> {
<a name="l00050"></a>00050                 <span class="keywordflow">return</span> ticks;
<a name="l00051"></a>00051         }
<a name="l00052"></a>00052 }
<a name="l00053"></a>00053 
<a name="l00062"></a><a class="code" href="_s_d_l2__framerate_8h.html#a843f0672446aff01ef03bbcd977fbedf">00062</a> <span class="keywordtype">void</span> <a class="code" href="_s_d_l2__framerate_8c.html#a444ebaaaa6b1ceeafa921562bdab1a44" title="Initialize the framerate manager.">SDL_initFramerate</a>(<a class="code" href="struct_f_p_smanager.html" title="Structure holding the state and timing information of the framerate controller.">FPSmanager</a> * manager)
<a name="l00063"></a>00063 {
<a name="l00064"></a>00064         <span class="comment">/*</span>
<a name="l00065"></a>00065 <span class="comment">        * Store some sane values </span>
<a name="l00066"></a>00066 <span class="comment">        */</span>
<a name="l00067"></a>00067         manager-&gt;<a class="code" href="struct_f_p_smanager.html#a05019faae5de8486dbb69d24b24eed78">framecount</a> = 0;
<a name="l00068"></a>00068         manager-&gt;<a class="code" href="struct_f_p_smanager.html#af7eec7342fd8eb48d5f49ba7f5b91853">rate</a> = <a class="code" href="_s_d_l2__framerate_8h.html#ab3ac02e6acb348129a019b9f20aa5c90" title="Default rate of framerate controller in Hz (1/s).">FPS_DEFAULT</a>;
<a name="l00069"></a>00069         manager-&gt;<a class="code" href="struct_f_p_smanager.html#a48695d55d40900a197688bb773b9d431">rateticks</a> = (1000.0f / (float) <a class="code" href="_s_d_l2__framerate_8h.html#ab3ac02e6acb348129a019b9f20aa5c90" title="Default rate of framerate controller in Hz (1/s).">FPS_DEFAULT</a>);
<a name="l00070"></a>00070         manager-&gt;<a class="code" href="struct_f_p_smanager.html#ad853f9c03e200a77fd897c1ea3aec43d">baseticks</a> = <a class="code" href="_s_d_l2__framerate_8c.html#a3bed31ab61648f7d69c8f47c90161cfe" title="Internal wrapper to SDL_GetTicks that ensures a non-zero return value.">_getTicks</a>();
<a name="l00071"></a>00071         manager-&gt;<a class="code" href="struct_f_p_smanager.html#aa7716779b7cc57dee24c15d7c5d62e55">lastticks</a> = manager-&gt;<a class="code" href="struct_f_p_smanager.html#ad853f9c03e200a77fd897c1ea3aec43d">baseticks</a>;
<a name="l00072"></a>00072 
<a name="l00073"></a>00073 }
<a name="l00074"></a>00074 
<a name="l00086"></a><a class="code" href="_s_d_l2__framerate_8h.html#aa2f7f11e60d81489392707faf07c5ac5">00086</a> <span class="keywordtype">int</span> <a class="code" href="_s_d_l2__framerate_8c.html#afad4b503cf9719daced45fa4d9653d72" title="Set the framerate in Hz.">SDL_setFramerate</a>(<a class="code" href="struct_f_p_smanager.html" title="Structure holding the state and timing information of the framerate controller.">FPSmanager</a> * manager, Uint32 rate)
<a name="l00087"></a>00087 {
<a name="l00088"></a>00088         <span class="keywordflow">if</span> ((rate &gt;= <a class="code" href="_s_d_l2__framerate_8h.html#a9555dceeaaffdd2669c991e6a300085b" title="Lowest possible rate supported by framerate controller in Hz (1/s).">FPS_LOWER_LIMIT</a>) &amp;&amp; (rate &lt;= <a class="code" href="_s_d_l2__framerate_8h.html#aeaeac0f0b439344496e29abf60904d58" title="Highest possible rate supported by framerate controller in Hz (1/s).">FPS_UPPER_LIMIT</a>)) {
<a name="l00089"></a>00089                 manager-&gt;<a class="code" href="struct_f_p_smanager.html#a05019faae5de8486dbb69d24b24eed78">framecount</a> = 0;
<a name="l00090"></a>00090                 manager-&gt;<a class="code" href="struct_f_p_smanager.html#af7eec7342fd8eb48d5f49ba7f5b91853">rate</a> = rate;
<a name="l00091"></a>00091                 manager-&gt;<a class="code" href="struct_f_p_smanager.html#a48695d55d40900a197688bb773b9d431">rateticks</a> = (1000.0f / (float) rate);
<a name="l00092"></a>00092                 <span class="keywordflow">return</span> (0);
<a name="l00093"></a>00093         } <span class="keywordflow">else</span> {
<a name="l00094"></a>00094                 <span class="keywordflow">return</span> (-1);
<a name="l00095"></a>00095         }
<a name="l00096"></a>00096 }
<a name="l00097"></a>00097 
<a name="l00107"></a><a class="code" href="_s_d_l2__framerate_8h.html#aebe43457dbb9fbfec6de18e7adf49e21">00107</a> <span class="keywordtype">int</span> <a class="code" href="_s_d_l2__framerate_8c.html#a575bb511d6f817ad846a788cbd08ae91" title="Return the current target framerate in Hz.">SDL_getFramerate</a>(<a class="code" href="struct_f_p_smanager.html" title="Structure holding the state and timing information of the framerate controller.">FPSmanager</a> * manager)
<a name="l00108"></a>00108 {
<a name="l00109"></a>00109         <span class="keywordflow">if</span> (manager == NULL) {
<a name="l00110"></a>00110                 <span class="keywordflow">return</span> (-1);
<a name="l00111"></a>00111         } <span class="keywordflow">else</span> {
<a name="l00112"></a>00112                 <span class="keywordflow">return</span> ((<span class="keywordtype">int</span>)manager-&gt;<a class="code" href="struct_f_p_smanager.html#af7eec7342fd8eb48d5f49ba7f5b91853">rate</a>);
<a name="l00113"></a>00113         }
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 
<a name="l00126"></a><a class="code" href="_s_d_l2__framerate_8h.html#a41de3f516b9633f102d7ba2e85d2bb98">00126</a> <span class="keywordtype">int</span> <a class="code" href="_s_d_l2__framerate_8c.html#a96b13e26f8436222e866904a592a6eec" title="Return the current framecount.">SDL_getFramecount</a>(<a class="code" href="struct_f_p_smanager.html" title="Structure holding the state and timing information of the framerate controller.">FPSmanager</a> * manager)
<a name="l00127"></a>00127 {
<a name="l00128"></a>00128         <span class="keywordflow">if</span> (manager == NULL) {
<a name="l00129"></a>00129                 <span class="keywordflow">return</span> (-1);
<a name="l00130"></a>00130         } <span class="keywordflow">else</span> {
<a name="l00131"></a>00131                 <span class="keywordflow">return</span> ((<span class="keywordtype">int</span>)manager-&gt;<a class="code" href="struct_f_p_smanager.html#a05019faae5de8486dbb69d24b24eed78">framecount</a>);
<a name="l00132"></a>00132         }
<a name="l00133"></a>00133 }
<a name="l00134"></a>00134 
<a name="l00146"></a><a class="code" href="_s_d_l2__framerate_8h.html#afc3b999d59d913771cd2588299096274">00146</a> Uint32 <a class="code" href="_s_d_l2__framerate_8c.html#afce13fa3dd37130deb4975d8b230c3ba" title="Delay execution to maintain a constant framerate and calculate fps.">SDL_framerateDelay</a>(<a class="code" href="struct_f_p_smanager.html" title="Structure holding the state and timing information of the framerate controller.">FPSmanager</a> * manager)
<a name="l00147"></a>00147 {
<a name="l00148"></a>00148         Uint32 current_ticks;
<a name="l00149"></a>00149         Uint32 target_ticks;
<a name="l00150"></a>00150         Uint32 the_delay;
<a name="l00151"></a>00151         Uint32 time_passed = 0;
<a name="l00152"></a>00152 
<a name="l00153"></a>00153         <span class="comment">/*</span>
<a name="l00154"></a>00154 <span class="comment">        * No manager, no delay</span>
<a name="l00155"></a>00155 <span class="comment">        */</span>
<a name="l00156"></a>00156         <span class="keywordflow">if</span> (manager == NULL) {
<a name="l00157"></a>00157                 <span class="keywordflow">return</span> 0;
<a name="l00158"></a>00158         }
<a name="l00159"></a>00159 
<a name="l00160"></a>00160         <span class="comment">/*</span>
<a name="l00161"></a>00161 <span class="comment">        * Initialize uninitialized manager </span>
<a name="l00162"></a>00162 <span class="comment">        */</span>
<a name="l00163"></a>00163         <span class="keywordflow">if</span> (manager-&gt;<a class="code" href="struct_f_p_smanager.html#ad853f9c03e200a77fd897c1ea3aec43d">baseticks</a> == 0) {
<a name="l00164"></a>00164                 <a class="code" href="_s_d_l2__framerate_8c.html#a444ebaaaa6b1ceeafa921562bdab1a44" title="Initialize the framerate manager.">SDL_initFramerate</a>(manager);
<a name="l00165"></a>00165         }
<a name="l00166"></a>00166 
<a name="l00167"></a>00167         <span class="comment">/*</span>
<a name="l00168"></a>00168 <span class="comment">        * Next frame </span>
<a name="l00169"></a>00169 <span class="comment">        */</span>
<a name="l00170"></a>00170         manager-&gt;<a class="code" href="struct_f_p_smanager.html#a05019faae5de8486dbb69d24b24eed78">framecount</a>++;
<a name="l00171"></a>00171 
<a name="l00172"></a>00172         <span class="comment">/*</span>
<a name="l00173"></a>00173 <span class="comment">        * Get/calc ticks </span>
<a name="l00174"></a>00174 <span class="comment">        */</span>
<a name="l00175"></a>00175         current_ticks = <a class="code" href="_s_d_l2__framerate_8c.html#a3bed31ab61648f7d69c8f47c90161cfe" title="Internal wrapper to SDL_GetTicks that ensures a non-zero return value.">_getTicks</a>();
<a name="l00176"></a>00176         time_passed = current_ticks - manager-&gt;<a class="code" href="struct_f_p_smanager.html#aa7716779b7cc57dee24c15d7c5d62e55">lastticks</a>;
<a name="l00177"></a>00177         manager-&gt;<a class="code" href="struct_f_p_smanager.html#aa7716779b7cc57dee24c15d7c5d62e55">lastticks</a> = current_ticks;
<a name="l00178"></a>00178         target_ticks = manager-&gt;<a class="code" href="struct_f_p_smanager.html#ad853f9c03e200a77fd897c1ea3aec43d">baseticks</a> + (Uint32) ((<span class="keywordtype">float</span>) manager-&gt;<a class="code" href="struct_f_p_smanager.html#a05019faae5de8486dbb69d24b24eed78">framecount</a> * manager-&gt;<a class="code" href="struct_f_p_smanager.html#a48695d55d40900a197688bb773b9d431">rateticks</a>);
<a name="l00179"></a>00179 
<a name="l00180"></a>00180         <span class="keywordflow">if</span> (current_ticks &lt;= target_ticks) {
<a name="l00181"></a>00181                 the_delay = target_ticks - current_ticks;
<a name="l00182"></a>00182                 SDL_Delay(the_delay);
<a name="l00183"></a>00183         } <span class="keywordflow">else</span> {
<a name="l00184"></a>00184                 manager-&gt;<a class="code" href="struct_f_p_smanager.html#a05019faae5de8486dbb69d24b24eed78">framecount</a> = 0;
<a name="l00185"></a>00185                 manager-&gt;<a class="code" href="struct_f_p_smanager.html#ad853f9c03e200a77fd897c1ea3aec43d">baseticks</a> = <a class="code" href="_s_d_l2__framerate_8c.html#a3bed31ab61648f7d69c8f47c90161cfe" title="Internal wrapper to SDL_GetTicks that ensures a non-zero return value.">_getTicks</a>();
<a name="l00186"></a>00186         }
<a name="l00187"></a>00187 
<a name="l00188"></a>00188         <span class="keywordflow">return</span> time_passed;
<a name="l00189"></a>00189 }
</pre></div></div><!-- contents -->


<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.0
</small></address>

</body>
</html>
